// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   BarPlotter.java

package com.java4less.rchart;

import com.java4less.rchart.gc.ChartColor;
import com.java4less.rchart.gc.ChartGraphics;
import com.java4less.rchart.gc.GraphicsProvider;
import com.java4less.rchart.gc.Polygon;
import java.util.Vector;

// Referenced classes of package com.java4less.rchart:
//			Plotter, LineStyle, DataSerie, BarDataSerie, 
//			ChartLabel, Scale, FillStyle

public class BarPlotter extends Plotter
{

	public boolean verticalBars;
	public int barWidth;
	private int originalBarWidth;
	public int interBarSpace;
	public double InterGroupSpace;
	public boolean cumulative;
	public boolean summedLabels;
	public boolean eventChart;
	public double barsBase;
	public boolean cumulativeBackwardsCompatible;
	public boolean barAtAxis;
	private int labelsPos[];

	public BarPlotter()
	{
		verticalBars = true;
		barWidth = 0;
		originalBarWidth = -1;
		interBarSpace = 0;
		InterGroupSpace = 0.10000000000000001D;
		cumulative = false;
		summedLabels = true;
		eventChart = false;
		barsBase = 0.0D;
		cumulativeBackwardsCompatible = false;
		barAtAxis = false;
		labelsPos = null;
	}

	private void sortCorners(int Xs[], int Ys[], int Xn[], int Yn[])
	{
		int minX = Xs[0];
		int maxX = Xs[0];
		int maxY = Ys[0];
		int minY = Ys[0];
		for (int i = 1; i < 4; i++)
		{
			if (Xs[i] < minX)
				minX = Xs[i];
			if (Ys[i] < minY)
				minY = Ys[i];
			if (Xs[i] > maxX)
				maxX = Xs[i];
			if (Ys[i] > maxY)
				maxY = Ys[i];
		}

		Xn[0] = minX;
		Yn[0] = maxY;
		Xn[1] = maxX;
		Yn[1] = maxY;
		Xn[2] = maxX;
		Yn[2] = minY;
		Xn[3] = minX;
		Yn[3] = minY;
	}

	protected void plotSerie(ChartGraphics g, DataSerie s, int serieSec)
	{
		LineStyle blackLine = new LineStyle(0.2F, GraphicsProvider.getColor(ChartColor.BLACK), 1);
		LineStyle whiteLine = new LineStyle(0.2F, GraphicsProvider.getColor(ChartColor.WHITE), 1);
		LineStyle grayLine = new LineStyle(0.2F, GraphicsProvider.getColor(ChartColor.DARKGRAY), 1);
		int seriesCount = 0;
		int serieOrder = serieSec;
		for (int i = 0; i < series.size(); i++)
		{
			DataSerie tmpSerie = (DataSerie)series.elementAt(i);
			if (!inSameSubChart(tmpSerie, s))
				continue;
			if (tmpSerie == s)
				serieSec = seriesCount;
			seriesCount++;
		}

		Scale tmpScaleX = super.getActiveXScale(s);
		if (cumulative || eventChart)
		{
			seriesCount = 1;
			serieSec = 0;
			interBarSpace = 0;
		}
		boolean isNegative = false;
		Scale scaleSerie = null;
		Scale scaleValue = null;
		if (verticalBars)
		{
			scaleSerie = super.getActiveXScale(s);
			scaleValue = super.getActiveYScale(s);
		} else
		{
			scaleSerie = super.getActiveYScale(s);
			scaleValue = super.getActiveXScale(s);
		}
		s.hotAreas.removeAllElements();
		BarDataSerie b;
		if (s instanceof BarDataSerie)
			b = (BarDataSerie)s;
		else
			return;
		if (serieOrder == 0)
			labelsPos = new int[s.getSize()];
		int GroupInterval = scaleSerie.getScreenCoord(scaleSerie.min + 1.0D) - scaleSerie.getScreenCoord(scaleSerie.min);
		if (GroupInterval < 0)
			GroupInterval *= -1;
		if (originalBarWidth == -1)
			originalBarWidth = barWidth;
		barWidth = originalBarWidth;
		int GroupWidth;
		if (barWidth == 0)
		{
			if (InterGroupSpace < 1.0D)
				GroupWidth = (int)((double)GroupInterval * (1.0D - InterGroupSpace));
			else
				GroupWidth = (int)((double)GroupInterval - InterGroupSpace);
			barWidth = GroupWidth / seriesCount - interBarSpace;
		}
		GroupWidth = (barWidth + interBarSpace) * seriesCount;
		int toCenterGroup = (GroupInterval - GroupWidth) / 2;
		if (toCenterGroup < 0)
			toCenterGroup = 0;
		int barx = 0;
		barx = (barWidth + interBarSpace) * serieSec;
		barx = (barx + toCenterGroup) - GroupInterval / 2;
		if (!verticalBars)
			barx *= -1;
		int barSizeHeight = 0;
		int GroupBase = 0;
		int count = b.getSize();
		int scSerie = 0;
		int sc = 0;
		Scale tmpScaleY = super.getActiveYScale(s);
		int Xs[] = new int[4];
		int Ys[] = new int[4];
		int Xn[] = new int[4];
		int Yn[] = new int[4];
		int x1 = 0;
		int y1 = 0;
		int x2 = 0;
		int y2 = 0;
		for (int i = 0; i < count; i++)
		{
			double XValue = ((Double)b.getElementX(i)).doubleValue();
			double YValue = ((Double)b.getElementY(i)).doubleValue();
			double tmpbarsBase = barsBase;
			if (b.barStart.size() > i)
				tmpbarsBase = ((Double)b.barStart.elementAt(i)).doubleValue();
			double stackedBarBase = tmpbarsBase;
			if (cumulative && !cumulativeBackwardsCompatible)
			{
				for (int k = serieOrder + 1; k < series.size(); k++)
				{
					BarDataSerie tmpBarSerie = (BarDataSerie)series.elementAt(k);
					if (!inSameSubChart(tmpBarSerie, s))
						continue;
					double d = ((Double)tmpBarSerie.getElementY(i)).doubleValue();
					if (YValue < 0.0D && d < 0.0D || YValue >= 0.0D && d >= 0.0D)
					{
						YValue += d;
						stackedBarBase += ((Double)tmpBarSerie.getElementY(i)).doubleValue();
					}
				}

				YValue += tmpbarsBase;
				tmpbarsBase = stackedBarBase;
			}
			GroupBase = scaleSerie.getScreenCoord(XValue + 1.0D);
			if (barAtAxis)
				GroupBase = scaleSerie.getScreenCoord(XValue);
			if (!verticalBars)
			{
				double t = XValue;
				XValue = YValue;
				YValue = t;
			}
			int scX = tmpScaleX.getScreenCoord(XValue);
			int scY = tmpScaleY.getScreenCoord(YValue);
			FillStyle styletoUse = b.style;
			isNegative = false;
			if (verticalBars)
			{
				barSizeHeight = scY;
				Xs[0] = GroupBase + barx;
				Ys[0] = tmpScaleY.getScreenCoord(tmpbarsBase);
				Xs[1] = GroupBase + barx + barWidth;
				Ys[1] = tmpScaleY.getScreenCoord(tmpbarsBase);
				Xs[2] = GroupBase + barx + barWidth;
				Ys[2] = barSizeHeight;
				Xs[3] = GroupBase + barx;
				Ys[3] = barSizeHeight;
				x1 = Xs[3];
				y1 = Ys[3];
				x2 = Xs[1];
				y2 = Ys[1];
				if (YValue < 0.0D)
				{
					if (b.negativeStyle != null)
						styletoUse = b.negativeStyle;
					isNegative = true;
					x1 = Xs[0];
					y1 = Ys[0];
					x2 = Xs[2];
					y2 = Ys[2];
				}
			} else
			{
				barSizeHeight = scX;
				Ys[0] = GroupBase + barx;
				Xs[0] = tmpScaleX.getScreenCoord(tmpbarsBase);
				Ys[3] = (GroupBase + barx) - barWidth;
				Xs[3] = tmpScaleX.getScreenCoord(tmpbarsBase);
				Ys[2] = (GroupBase + barx) - barWidth;
				Xs[2] = barSizeHeight;
				Ys[1] = GroupBase + barx;
				Xs[1] = barSizeHeight;
				x1 = Xs[3];
				y1 = Ys[3];
				x2 = Xs[1];
				y2 = Ys[1];
				if (XValue < 0.0D)
				{
					if (b.negativeStyle != null)
						styletoUse = b.negativeStyle;
					isNegative = true;
					x1 = Xs[2];
					y1 = Ys[2];
					x2 = Xs[0];
					y2 = Ys[0];
				}
			}
			if (b.barStyles != null && b.barStyles.length > i)
				styletoUse = b.barStyles[i];
			Polygon p = new Polygon();
			p.addPoint(Xs[0], Ys[0]);
			p.addPoint(Xs[1], Ys[1]);
			p.addPoint(Xs[2], Ys[2]);
			p.addPoint(Xs[3], Ys[3]);
			s.hotAreas.addElement(p);
			styletoUse.draw(g, x1, y1, x2, y2);
			LineStyle border1 = grayLine;
			LineStyle border2 = whiteLine;
			if (b.border != null)
				border1 = b.border;
			if (b.border2 != null)
				border2 = b.border2;
			if (isNegative && b.negativeborder2 != null)
				border2 = b.negativeborder2;
			if (isNegative && b.negativeborder != null)
				border1 = b.negativeborder;
			if (border1 != null && b.borderType == 0)
				border1.drawPolygon(g, Xs, Ys, 4);
			sortCorners(Xs, Ys, Xn, Yn);
			if (b.borderType == 2)
			{
				if (border2 != null)
				{
					border2.draw(g, Xn[2], Yn[2], Xn[3], Yn[3]);
					border2.draw(g, Xn[3], Yn[3], Xn[0], Yn[0]);
				}
				if (border1 != null)
				{
					border1.draw(g, Xn[0], Yn[0], Xn[1], Yn[1]);
					border1.draw(g, Xn[1], Yn[1], Xn[2], Yn[2]);
				}
			}
			if (b.borderType == 1)
			{
				if (border1 != null)
				{
					border2.draw(g, Xn[0], Yn[0], Xn[1], Yn[1]);
					border2.draw(g, Xn[1], Yn[1], Xn[2], Yn[2]);
				}
				if (border2 != null)
				{
					border1.draw(g, Xn[2], Yn[2], Xn[3], Yn[3]);
					border1.draw(g, Xn[3], Yn[3], Xn[0], Yn[0]);
				}
			}
			if (b.valueFont == null)
				continue;
			g.setColor(b.valueColor);
			g.setFont(b.valueFont);
			int toCenterValue = 0;
			String txt;
			String txtValue;
			ChartLabel labelf;
			if (verticalBars)
			{
				if (!summedLabels)
					YValue = ((Double)b.getElementY(i)).doubleValue();
				txt = b.doubleToString(new Double(YValue));
				if (cumulative && ((Double)b.getElementY(i)).doubleValue() == 0.0D)
					txt = "";
				txtValue = txt;
				if (b.labelTemplate.length() > 0)
					txt = b.labelTemplate;
				if (b.dataLabels != null && b.dataLabels.length > i)
					txt = b.dataLabels[i];
				labelf = new ChartLabel(txt, txtValue, false, false);
				labelf.initialize(g, super.chart);
				toCenterValue = labelf.requiredWidth / 2;
				if (toCenterValue < 0)
					toCenterValue = 0;
				if (!isNegative)
				{
					int tmpY = barSizeHeight - 4 - labelf.requiredHeight;
					if (labelsPos.length > i && cumulative)
					{
						if (serieOrder > 0 && tmpY - labelf.requiredHeight < labelsPos[i] && super.inSameSubChart((DataSerie)series.elementAt(serieOrder - 1), s))
							tmpY = labelsPos[i] - labelf.requiredHeight;
						if (txt.trim().length() > 0)
							labelsPos[i] = tmpY;
					}
					labelf.paint(g, GroupBase + barx + toCenterValue, tmpY, -1, -1);
				} else
				{
					labelf.paint(g, GroupBase + barx + toCenterValue, barSizeHeight + 4, -1, -1);
				}
				continue;
			}
			if (!summedLabels)
				XValue = ((Double)b.getElementY(i)).doubleValue();
			txt = b.doubleToString(new Double(XValue));
			if (eventChart)
			{
				txt = b.doubleToString(new Double(XValue - tmpbarsBase));
				if (XValue - tmpbarsBase == (double)(int)XValue - tmpbarsBase)
					txt = (new Integer((int)(XValue - tmpbarsBase))).toString();
			} else
			if (XValue == (double)(int)XValue)
				txt = (new Integer((int)XValue)).toString();
			if (cumulative && ((Double)b.getElementY(i)).doubleValue() == 0.0D)
				txt = "";
			txtValue = txt;
			if (b.labelTemplate.length() > 0)
				txt = b.labelTemplate;
			if (b.dataLabels != null && b.dataLabels.length > i)
				txt = b.dataLabels[i];
			labelf = new ChartLabel(txt, txtValue, false, false);
			labelf.initialize(g, super.chart);
			toCenterValue = (Math.abs(barWidth) - labelf.requiredHeight) / 2;
			if (toCenterValue < 0)
				toCenterValue = 0;
			if (eventChart)
			{
				labelf.paint(g, barSizeHeight - 4 - labelf.requiredWidth, GroupBase - barx - labelf.requiredHeight, -1, -1);
				continue;
			}
			if (!isNegative)
				labelf.paint(g, barSizeHeight + 4, (GroupBase + barx) - barWidth, -1, -1);
			else
				labelf.paint(g, barSizeHeight - 4 - labelf.requiredWidth, (GroupBase + barx) - barWidth, -1, -1);
		}

	}

	public void addSerie(DataSerie s)
	{
		Scale tmpScaleY = super.getActiveYScale(s);
		Scale tmpScaleX = super.getActiveXScale(s);
		if (!verticalBars)
		{
			double t = tmpScaleX.max;
			tmpScaleX.max = tmpScaleY.max;
			tmpScaleY.max = t;
			t = tmpScaleX.min;
			tmpScaleX.min = tmpScaleY.min;
			tmpScaleY.min = t;
		}
		super.addSerie(s);
		if (!verticalBars)
		{
			double t = tmpScaleX.max;
			tmpScaleX.max = tmpScaleY.max;
			tmpScaleY.max = t;
			t = tmpScaleX.min;
			tmpScaleX.min = tmpScaleY.min;
			tmpScaleY.min = t;
		}
	}
}
